package com.settlement.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.settlement.system.mapper.SysAppVersionMapper;
import com.settlement.system.model.entity.SysAppVersion;
import com.settlement.system.model.form.SysAppVersionForm;
import com.settlement.system.model.query.SysAppVersionQuery;
import com.settlement.system.model.vo.SysAppVersionImportVo;
import com.settlement.system.service.SysAppVersionService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * APP版本表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-01-11
 */
@Service
public class SysAppVersionServiceImpl extends ServiceImpl<SysAppVersionMapper, SysAppVersion> implements SysAppVersionService {

    @Override
    public Page<SysAppVersion> getPage(SysAppVersionQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<SysAppVersion> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

    @Override
    public boolean saveSysAppVersion(SysAppVersionForm sysAppVersionForm) {
        SysAppVersion sysAppVersion = new SysAppVersion();
        BeanUtil.copyProperties(sysAppVersionForm, sysAppVersion);
        return this.saveOrUpdate(sysAppVersion);
    }

    @Override
    public SysAppVersionForm getSysAppVersionFormData(Long id) {
        SysAppVersion sysAppVersion = this.getById(id);
        SysAppVersionForm sysAppVersionForm = new SysAppVersionForm();
        BeanUtil.copyProperties(sysAppVersion, sysAppVersionForm);
        return sysAppVersionForm;
    }

    @Override
    public boolean deleteSysAppVersion(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
    }

    @Override
    public void importData(List<SysAppVersionImportVo> voList) {
        List<SysAppVersion> sysAppVersionList = new ArrayList<>();
        for (SysAppVersionImportVo importVo : voList) {
            SysAppVersion sysAppVersion = new SysAppVersion();
            BeanUtil.copyProperties(importVo, sysAppVersion);
            sysAppVersionList.add(sysAppVersion);
        }
        this.saveOrUpdateBatch(sysAppVersionList);
    }

    @Override
    public List<SysAppVersionImportVo> findByIds(String idsStr) {
        if (StrUtil.isBlank(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<SysAppVersion> sysAppVersionList = this.getBaseMapper().selectBatchIds(ids);
        List<SysAppVersionImportVo> voList = new ArrayList<>();
        for (SysAppVersion sysAppVersion : sysAppVersionList) {
            SysAppVersionImportVo vo = new SysAppVersionImportVo();
            BeanUtil.copyProperties(sysAppVersion, vo);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public String checkVersion(int version) {
        // 获取所有的版本
        List<SysAppVersion> appVersions = this.list();
        SysAppVersion sysAppVersion = appVersions.stream().filter(versionObj -> versionObj.getAppVersion() > version)
                .findFirst().orElse(null);
        if (sysAppVersion != null) {
            return sysAppVersion.getFilePath();
        }
        return null;
    }
}
